package com.example.day37_systemcameravideo;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends Activity {

	private ImageView iv;
	private VideoView vv;

	String path =Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()+File.separator+"new.jpg";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		iv = (ImageView) findViewById(R.id.iv);
		vv = (VideoView) findViewById(R.id.vv);
	}

	public void creame(View v){
		Intent intent  = new Intent();
		intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
		File file = new File(path);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
		startActivityForResult(intent, 0);
	}
	public void video(View v){
		Intent intent = new Intent();
		intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
		
		startActivityForResult(intent, 1);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode==0&&resultCode==RESULT_OK) {
			Bitmap bitmap = BitmapFactory.decodeFile(path);
			iv.setImageBitmap(bitmap);
			iv.setVisibility(View.VISIBLE);
		}
		if (requestCode==1&&resultCode==RESULT_OK) {
			Uri uri = data.getData();
			Cursor cursor = getContentResolver().query(uri, null, null, null, null);
			while(cursor.moveToNext()){
				String path = cursor.getString(cursor.getColumnIndex("_data"));
				
				vv.setVisibility(View.VISIBLE);
				vv.setVideoPath(path);
				vv.setMediaController(new MediaController(this));
				vv.start();
			}
			cursor.close();
		}
		
	}
}
